/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lianxishilili.jhelper;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import lianxishilili.demo.model.AddressBookBean;

/**
 *
 * @author hp
 */
public class AddressBookPanel extends JPanel {
    Panel p,pleft,pleft_main,pleft_down;
    JPanel pQuery;
    JScrollPane sp;
    JList lall;
    DefaultListModel listmodo;
    JButton btnQuery,btnAdd,btnSava,btnDel;
    JTextField txtName,txtOffice,txtHome,txtMobile,txtQQ,txtEmail,txtOther,txtCondition;
    JLabel lName,lOffice,lHome,lMobile,lQQ,lEmail,lOther,lMsg;
    Hashtable h;
    public AddressBookPanel(){
        p=new Panel();
        pleft=new Panel();
        pleft_main=new Panel();
        pleft_down=new Panel();
        pQuery=new JPanel();
        btnQuery=new JButton("查询");
        btnSava=new JButton("保存");
        btnDel=new JButton("删除");
        btnAdd=new JButton("添加");
        txtName=new JTextField("",20);
        txtName.setEditable(false);
        txtOffice=new JTextField("",20);
        txtHome=new JTextField("",20);
        txtMobile=new JTextField("",20);
        txtQQ=new JTextField("",20);
        txtEmail=new JTextField("",20);
        txtOther=new JTextField("",20);
        txtCondition=new JTextField("",10);
        txtName=new JTextField("",20);
        lName=new JLabel("姓名",JLabel.CENTER);
        lOffice=new JLabel("办公电话",JLabel.CENTER);
        lHome=new JLabel("家庭电话",JLabel.CENTER);
        lMobile=new JLabel("手机",JLabel.CENTER);
        lQQ=new JLabel("QQ",JLabel.CENTER);
        lEmail=new JLabel("Email",JLabel.CENTER);
        lOther=new JLabel("其它",JLabel.CENTER);
        lMsg=new JLabel("请输入要查询的联系人姓名：");
        
        sp=new JScrollPane();
        h=new AddressBooks().getAllCards();
        listmodo=new DefaultListModel();
        if(h!=null){
            listmodo.addElement("点击如下名单可查看相关信息");
            Enumeration e=h.keys();
            while(e.hasMoreElements()){
                listmodo.addElement(e.nextElement());
              }
         }else{
            listmodo.addElement("通讯录为空");
            txtName.setEditable(true);
           }
        lall=new JList(listmodo);
        sp.setViewportView(lall);
        pQuery.setBorder(BorderFactory.createTitledBorder("联系人查询"));
        pQuery.setLayout(new FlowLayout(FlowLayout.CENTER));
        pQuery.add(lMsg);
        pQuery.add(txtCondition);
        pQuery.add(btnQuery);
        pleft_main.setLayout(New GridLayout(7,2));
        pleft_main.add(lName);
        pleft_main.add(txtName);
        pleft_main.add(lMobile);
        pleft_main.add(txtMobile);
        pleft_main.add(lOffice);
        pleft_main.add(txtOffice);
        pleft_main.add(lHome);
        pleft_main.add(txtHome);
        pleft_main.add(lQQ);
        pleft_main.add(txtQQ);
        pleft_main.add(lEmail);
        pleft_main.add(txtEmail);
        pleft_main.add(lOther);
        pleft_main.add(txtOther);
        p.setLayout(new FlowLayout(FlowLayout.CENTER));
        p.add(pleft_main);
        pleft_down.setLayout(new FlowLayout(FlowLayout.CENTER));
        pleft_down.add(pQuery);
        pleft_down.add(btnAdd);
        pleft_down.add(btnSava);
        pleft_down.add(btnDel);
        pleft.setLayout(new BorderLayout());
        pleft.add("Center",p);
        pleft.add("South",pleft_down);
        this.setLayout(new BorderLayout());
        this.add("East",sp);
        this.add("Center",pleft);
        if(h!=null){
            lall.setSelectedIndex(l);
            showCard(lall.getSelectedValue().toString());
            lall.addListSelectionListener(new ListSelectionListener() {

                @Override
                public void valueChanged(ListSelectionEvent e) {
                    int index=lall.getSelectedIndex();
                    Object name=lall.getSelectedValue();
                    if(index>0){
                        showCard(name.toString());
                    }
                }
            });
            btnAdd.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    clearText();
                }
            });
              btnSava.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if(txtName.isEnabled()==true){
                        addNew();
                    }else{
                        update();
                     }
                    refreshList();
                }
            });
               btnDel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                   delete();
                   refreshList();
                }
            });
                btnQuery.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                   String name=txtCondition.getText();
                   showCard(name);
                }
            });
          }
        public void delete(){
            if(txtName.getText().trim().length()==0){
                JOptionPane.showMessageDialog(null, "必须输入联系人姓名");
                return;
            }
            String name=txtName.getText();
            txtName.setEnabled(false);
            if(new AddressBooks().delCard(name)==true){
                JOptionPane.showMessageDialog(null, "成功删除了"+name+"的联系方式");
            }else{
                JOptionPane.showMessageDialog(null,"联系人"+name+"的信息删除失败");
            }
         }
        public void refreshList(){
            h=new AddressBooks().getAllCards();
            if(h!=null){
                listmodo.clear();
                listmodo.addElement("点击如下名单可查看相关信息");
                Enumeration e=h.keys();
                while(e.hasMoreElements()){
                    listmodo.addElement(e.nextElement());
                 }
            }else{
                listmodo.clear();
                listmodo.addElement("通讯录为空");
            }
        }
        public void showCard(String name){
            Object temp=h.get(name);
            if(temp==null){
                JOptionPane.showMessageDialog(null, "没有找到联系人："+name,"提示",JOptionPane.ERROR_MESSAGE);
                return;
                 }
            AddressBookBean b=(AddressBookBean)temp;
            txtName.setText(b.getName());
            txtName.setEditable(false);
            txtMobile.setText(b.getMobile());
            txtOffice.setText(b.getOffice());
            txtHome.setText(b.getHome());
            txtQQ.setText(b.getQQ());
            txtEmail.setText(b.getEmail());
            txtOther.setText(b.getOther());
        
        }
        public void clearText(){
            txtName.setText("");
            txtName.setEnabled(true);
            txtName.setEditable(true);
            txtMobile.setText("");
            txtOffice.setText("");
            txtHome.setText("");
            txtQQ.setText("");
            txtEmail.setText("");
            txtOther.setText("");
             }
        public void addNew(){
            if(txtName.getText().trim().length()==0){
                 JOptionPane.showMessageDialog(null,"必须输入联系人姓名");
                 return;
             }
            String name=txtName.getText();
            String mobile=txtMobile.getText();
            mobile=mobile.trim().length()==0?"空":mobile.trim();
            String office=txtOffice.getText();
            office=office.trim().length()==0?"空":office.trim();
            String home=txtHome.getText();
            home=home.trim().length()==0?"空":home.trim();
            String qq=txtQQ.getText();
            qq=qq.trim().length()==0?"空":qq.trim();
            String email=txtEmail.getText();
            email=email.trim().length()==0?"空":email.trim();
            String other=txtOther.getText();
            other=other.trim().length()==0?"空":mobile.trim();
            AddressBookBean newcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
            if(new AddressBooks().newCard(newcard)==true){
                JOptionPane.showMessageDialog(null, "成功添加了"+name+"的联系信息");
            }else{
                JOptionPane.showMessageDialog(null,"联系人"+name+"的信息添加失败");
              }
            txtName.setEnabled(false);
        }
        public void update(){
            if(txtName.getText().trim().length()==0){
                 JOptionPane.showMessageDialog(null,"必须输入联系人姓名");
                 return;
             }
            String name=txtName.getText();
            String mobile=txtMobile.getText();
            mobile=mobile.trim().length()==0?"空":mobile.trim();
            String office=txtOffice.getText();
            office=office.trim().length()==0?"空":office.trim();
            String home=txtHome.getText();
            home=home.trim().length()==0?"空":home.trim();
            String qq=txtQQ.getText();
            qq=qq.trim().length()==0?"空":qq.trim();
            String email=txtEmail.getText();
            email=email.trim().length()==0?"空":email.trim();
            String other=txtOther.getText();
            other=other.trim().length()==0?"空":mobile.trim();
            AddressBookBean oldcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
            if(new AddressBooks().modifyCard(oldcard)==true){
                JOptionPane.showMessageDialog(null, "成功修改了"+name+"的联系信息");
            }else{
                JOptionPane.showMessageDialog(null,"联系人"+name+"的信息修改失败");
              }
        }
        public void paintComponent(Graphics g){
            super.paintComponent(g);
        }
        public static void main(String[] args) {
        Frame f=new Frame();
        f.add(new AddressBookPanel());
        f.pack();
        f.show();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent E){
                System.exit(0);
             }
         });
    }
       }

 

